package com.shopx.cm.service.impl;


import com.shopx.cm.dao.db2.SysOrganizationDao;
import com.shopx.cm.dto.OrgDTO;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.exception.OriginException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.base.service.base.BaseService;

import javax.annotation.Resource;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.shopx.dal.redis.CacheServiceFactory;
import com.shopx.cm.bean.CmStore;
import com.shopx.cm.model.CmStoreModel;
import com.shopx.cm.dao.db1.CmStoreDao;
import com.shopx.cm.service.ICmStoreService;

/**
 * TODO
 *
 * @author lituo
 * @className: CmStore
 * @date 2025-01-08
 */
@Service
@Slf4j
public class CmStoreServiceImpl implements ICmStoreService {

    @Resource
    private CmStoreDao cmStoreDao;

    @Resource
    private SysOrganizationDao sysOrganizationDao;

    @Resource
    private ISysSeqService sysSeqService;

    @Override
    public CmStoreModel selectByPrimaryKey(CmStore cmStore) {
        if (cmStore == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return cmStoreDao.selectByPrimaryKey(cmStore);
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int insertSelective(CmStore cmStore) {
        if (cmStore == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        String id = sysSeqService.getTabSeq("CmStore", 4, null);
        cmStore.setCsId(id);
        int result = cmStoreDao.insertSelective(cmStore);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CmStore cmStore) {
        if (cmStore == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmStoreDao.updateByPrimaryKeySelective(cmStore);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int deleteByPrimaryKey(CmStore cmStore) {
        if (cmStore == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmStoreDao.deleteByPrimaryKey(cmStore);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(CmStoreModel cmStore) {
        return cmStoreDao.selectCount(cmStore);
    }

    @Override
    public List<CmStoreModel> selectList(CmStoreModel cmStore) {
        List<CmStoreModel> list = cmStoreDao.selectList(cmStore);
        return list;
    }

    @Override
    public PageDTO<CmStoreModel> getListPage(CmStoreModel cmStore) {
        if (cmStore == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<CmStoreModel> pageDTO = new PageDTO<CmStoreModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(cmStore));
        pageDTO.setData(selectList(cmStore));
        return pageDTO;
    }

    /**
     * 同步erp数据
     */
    public void syncErpData(){
        log.info("CmStoreService.syncErpDate.begin");
        List<OrgDTO> list = sysOrganizationDao.queryList();
        log.info("list.size:{}，准备同步数据",list.size());
        cmStoreDao.truncateTable();
        log.info("清空数据完成");
        cmStoreDao.insertBatch(list);
        log.info("同步数据完成");
    }
}

